﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FindService.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string category, string coordinates)
        {
            var polygon = JsonConvert.DeserializeObject<List<Location>>(coordinates);
            int k, j = polygon.Count - 1;
            var location = new Location() { lat = 10.841293399447757, lng = 106.65387868881226 };
            var isInside = false;
            for (k = 0; k < polygon.Count; k++)
            {
                Location polyK = polygon[k];
                Location polyJ = polygon[j];
                if (((polyK.lng > location.lng) != (polyJ.lng > location.lng)) &&
                 (location.lat < (polyJ.lat - polyK.lat) * (location.lng - polyK.lng) / (polyJ.lng - polyK.lng) + polyK.lat))
                    isInside = !isInside;
                j = k;
            }
            if (isInside)
            {
                return Json(new List<Location> { location }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }
    }

    public class Location
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
}
